Add multiple link support for GPX 1.1 on read and write.
authorrobertl <robertl>
Sun, 11 Mar 2007 15:33:55 +0000 (15:33 +0000)
committerrobertl <robertl>
Sun, 11 Mar 2007 15:33:55 +0000 (15:33 +0000)
defs.h
gpx.c
util.c

diff --git a/defs.h b/defs.h
index 3bfb8b31e20a7521e6ec1bb00c8b25ceadefaa35..527c1b025633fa47d2823f05c758595f521de845 100644 (file)
--- a/defs.h
+++ b/defs.h
@@ -276,12 +276,6 @@ typedef struct {
        unsigned int cet_converted:1;           /* strings are converted to UTF8; interesting only for input */
 } wp_flags;
 
-typedef struct url_link {
-       struct url_link *url_next;
-       char *url;
-       char *url_link_text;
-} url_link;
-
 /*
  * This is a waypoint, as stored in the GPSR.   It tries to not 
  * cater to any specific model or protocol.  Anything that needs to
@@ -336,6 +330,8 @@ typedef struct {
         * afterthought and I don't want to change our data structures.
         * So we have the first in the waypoint itself and subsequent
         * ones in a linked list.
+        * We also use an implicit anonymous union here, so these three
+        * members must match struct url_link...
         */
        struct url_link *url_next;
        char *url;
@@ -421,6 +417,19 @@ typedef struct {
        int request_terminate;
 } posn_status;
 
+/*
+ * Structures and functions for multiple URLs per waypoint.
+ */
+typedef struct url_link {
+       struct url_link *url_next;
+       char *url;
+       char *url_link_text;
+} url_link;
+
+void add_url(waypoint *wpt, char *link, char *url_link_text);
+
+
+
 typedef void (*ff_init) (char const *);
 typedef void (*ff_deinit) (void);
 typedef void (*ff_read) (void);
diff --git a/gpx.c b/gpx.c
index b204f5f5a2d967b73f691aa4c7aff2ed1b97696c..0dfeda32303248d05adb7bdd5c44dfe128145f6b 100644 (file)
--- a/gpx.c
+++ b/gpx.c
@@ -48,6 +48,8 @@ static int cache_descr_is_html;
 static gbfile *fd;
 static gbfile *ofd;
 static short_handle mkshort_handle;
+static const char *link_url;
+static char *link_text;
 
 static const char *input_string = NULL;
 static int input_string_len = 0;
@@ -599,9 +601,12 @@ gpx_start(void *data, const XML_Char *xml_el, const XML_Char **xml_attr)
                break;
        case tt_wpt_link:
                if (0 == strcmp(attr[0], "href")) {
-                       wpt_tmp->url = xstrdup(attr[1]);
+                       link_url = attr[1];
                }
                break;
+       case tt_wpt_link_text:
+               link_text = cdatastr.mem;
+               break;
        case tt_rte:
                rte_head = route_head_alloc();
                route_add_head(rte_head);
@@ -853,9 +858,19 @@ gpx_end(void *data, const XML_Char *xml_el)
                wpt_tmp->url = xstrdup(cdatastrp);
                break;
        case tt_wpt_urlname:
-       case tt_wpt_link_text:
                wpt_tmp->url_link_text = xstrdup(cdatastrp);
                break;
+       case tt_wpt_link: {
+               char *lt = link_text;
+               if (lt) {
+                       lt = xstrdup(lrtrim(link_text));
+               }
+               
+               fprintf(stderr, "Here %s/%s\n", link_url, lt);
+               add_url(wpt_tmp, xstrdup(link_url), lt);
+               link_text = NULL;
+               }
+               break;
        case tt_wpt:
                waypt_add(wpt_tmp);
                logpoint_ct = 0;
@@ -1396,21 +1411,27 @@ write_gpx_url(const waypoint *waypointp)
 {
        char *tmp_ent;
 
-       if (waypointp->url) {
-               tmp_ent = xml_entitize(waypointp->url);
-               if (gpx_wversion_num > 10) {
-                       
+       if (waypointp->url == NULL) {
+               return;
+       }
+
+       if (gpx_wversion_num > 10) {
+               url_link *tail;
+               for (tail = (url_link *)&waypointp->url_next; tail; tail = tail->url_next) {
+                       tmp_ent = xml_entitize(tail->url);
                        gbfprintf(ofd, "  <link href=\"%s%s\">\n", 
                                urlbase ? urlbase : "", tmp_ent);
                        write_optional_xml_entity(ofd, "  ", "text", 
-                               waypointp->url_link_text);
+                               tail->url_link_text);
                        gbfprintf(ofd, "  </link>\n");
-               } else {
-                       gbfprintf(ofd, "  <url>%s%s</url>\n", 
-                               urlbase ? urlbase : "", tmp_ent);
-                       write_optional_xml_entity(ofd, "  ", "urlname", 
-                               waypointp->url_link_text);
+                       xfree(tmp_ent);
                }
+       } else {
+               tmp_ent = xml_entitize(waypointp->url);
+               gbfprintf(ofd, "  <url>%s%s</url>\n", 
+                       urlbase ? urlbase : "", tmp_ent);
+               write_optional_xml_entity(ofd, "  ", "urlname", 
+                       waypointp->url_link_text);
                xfree(tmp_ent);
        }
 }
diff --git a/util.c b/util.c
index 7d32a2eb126e4598eddb7fca4e2be8c656298dd7..135574c6822fd40c48daa642906cc51c25bf7176 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1655,7 +1655,7 @@ add_url(waypoint *wpt, char *link, char *url_link_text)
                new_link->url = link;
                new_link->url_link_text = url_link_text;
 
-               /* Find current end of chain. */
+               /* Find current end of chain and tack this onto the end.. */
                for (tail = wpt->url_next;;tail = tail->url_next) {
                        if (tail == NULL) {
                                wpt->url_next = new_link;